var WxParse = require('../../wxParse/wxParse.js');

var app = getApp()
Page({

  /**
   * 页面的初始数
   */
  data: {
    flag: 0,    //小星星
    showView:false,
    shoppingCarPrice:0,
    goodsnumber:1,
    index:0,
    indicatorDots: true,
    autoplay: true,
    disable: true,
    showView: true,
    interval: 3000,
    duration: 1000,
    // banner_list: [
    //   {
    //     banner: [
    //       {
    //         "pri_url": "http://39.104.28.147:10001/shcypro/image/image/banner1.jpg"
    //       },
    //       {
    //         "pri_url": "http://39.104.28.147:10001/shcypro/image/image/banner2.jpg"
    //       }
    //     ]
    //   }
    // ]
  },
  recId: '',
  pageIndex:1,
  pageSize:100,
  evaluate:function(){
    wx.navigateTo({
      url: '../evaluate/evaluate',
    })
  },
  onLoad: function (options) {
    var that = this;
    const goodsId = options.goodsId
    console.log(options.goodsId)
    wx.request({
      url: app.globalData.url+'getGoodDetailByGoodsId',
      data: {
        goodsId: goodsId
      },
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
    
        that.setData({
          originalImg: res.data.object.originalImg,
          shopPrice: res.data.object.shopPrice,
          goodsName: res.data.object.goodsName,
          providerName: res.data.object.providerName,
          goodsWeight: res.data.object.goodsWeight,
          goodsBrief: res.data.object.goodsBrief,
        })
        var article = res.data.object.goodsDesc;
        WxParse.wxParse('article', 'html', article, that, 5);
        const goodIds = res.data.object.goodsId;
        that.goodsIds = goodIds

        const userId = app.globalData.userid
        /**
         * 详情页---购物车总价
         */
        wx.request({
          url: app.globalData.url+'getShopCart',
          data: {
            cartType: 2,
            userId: userId,
            pageSize:that.pageSize,
            pageIndex:that.pageIndex
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          method: 'GET',
          success: function(res) {
            if (res.data.resultMsg == "没有查到购物车数据信息"){
              console.log(res.data.resultMsg)
                that.setData({
                  shoppingCarPrice: 0
                })
            }else{
              var letsum = 0
              for (const carList of res.data.object.cartList) {
                var allSum = carList.goodsPrice * carList.goodsNumber
                letsum += allSum
              }
              that.setData({
                shoppingCarPrice: letsum.toFixed(0)
              })
            }
          },
          fail: function(res) {},
          complete: function(res) {},
        })
        /**
         * 查看商品是否收藏
         */
        wx.request({
          url: app.globalData.url+'checkGoodsIsCollect',
          data: {
            userId: userId,
            goodsId: that.goodsIds,
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          method: 'GET',
          success: function (res) {
            if (res.data.object[0] == true) {
              that.setData({
                flag: 1
              })
            } else {
              that.setData({
                flag: 0
              })
            }
            that.recId = res.data.object[1]
          },
          fail: function (res) { },
          complete: function (res) { },
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**
   * 参数点击
   */
  parametrLine:function(){
    this.setData({
      showView: !this.data.showView
    })
  },
  /**
   * 收藏--点击事件
   */
  collection:function(){
    var that = this;
    const goodsId = that.goodsIds;
    const userId = app.globalData.userid
    if( this.data.flag == 0 ){
      wx.request({
        url: app.globalData.url+'addGoodsCollect',
        data: {
          userId:userId,
          goodsId: goodsId,
          isAttention:0,
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        method: 'GET',
        success: function(res) {
          wx.showToast({
            title: '收藏成功',
            duration: 800,
            mask: true,
          })
          that.setData({
            flag: 1
          })
        },
        fail: function(res) {},
        complete: function(res) {},
      })
    }else{
      console.log(app.globalData.delRectid)
      wx.request({
        url: app.globalData.url+'removeGoodsCollect',
        data: {
          id: that.recId
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        method: 'GET',
        success: function(res) {
          wx.showToast({
            title: '取消收藏',
            duration: 800,
            mask: true,
          })
          that.setData({
            flag: 0
          })
        },
        fail: function(res) {},
        complete: function(res) {},
      })
    }
  },
  /**
   * 加入购物车
   */
  shoppingIn:function(e){
    var that = this;
    const goodsName = e.currentTarget.dataset.goodsname;
    const goodsPrice = e.currentTarget.dataset.goodsprice;
    const userId = app.globalData.userid
    const goodsPic = e.currentTarget.dataset.img;                                   
    wx.request({
      url: app.globalData.url+'insertGoodsInCartShop',
      data: {
        goodsNumber: 1,
        goodsPic: goodsPic,
        goodsName: goodsName,
        goodsPrice: goodsPrice,
        goodsId: that.goodsIds,
        cartType: 2,
        userId:userId,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function(res) {
        wx.showToast({
          title: '购物车添加成功',
          icon: 'success',
          duration: 1000,
          mask: true,
          success: function (res) {
            const shoppingPrice = e.currentTarget.dataset.prices;
            /**
             * 详情页---购物车总价
             */
            wx.request({
              url: app.globalData.url+'getShopCart',
              data: {
                cartType: 2,
                userId: userId,
                pageIndex:that.pageIndex,
                pageSize:that.pageSize
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              method: 'GET',
              success: function (res) {
                var letsum = 0
                for (const carList of res.data.object.cartList) {
                  var allSum = carList.goodsPrice * carList.goodsNumber
                  letsum += allSum
                }
                that.setData({
                  shoppingCarPrice: letsum.toFixed(0)
                })
              },
              fail: function (res) { },
              complete: function (res) { },
            })
          },
          fail: function (res) { },
          complete: function (res) { },
        })
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  shoppingCar:function(e){
    wx.navigateTo({
      url: '../shoppingCar/shoppingCar',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  buy:function(){
    wx.navigateTo({
      url: '../orderPay/orderPay',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  
})